package com.hexseek.libretvcloud.controller;


import com.hexseek.libretvcloud.model.Proxy;
import com.hexseek.libretvcloud.model.R;
import com.hexseek.libretvcloud.service.ProxyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhougb9
 * @version 1.0.0
 * @description TODO
 * @date 2025/5/14 18:17
 * @title CustomProxyController
 */
@RestController
@RequestMapping("/api/proxy")
@Slf4j
public class ProxyController {

    @Autowired
    private ProxyService proxyService;


    @GetMapping("/getDefaultProxy")
    public R<?> getDefaultProxy() {

        List<Proxy> allSources = proxyService.getDefaultProxy();

        return R.ok(allSources);
    }


    // 获取用户的自定义API源
    @GetMapping("getCustomProxy")
    public R<?> getCustomProxy(@CookieValue(value = "userId", required = false) String userId) {

        if (userId == null || userId.isEmpty()) {
            return R.fail("用户ID缺失");
        }

        List<Proxy> sources = proxyService.getCustomProxy(userId);

        return R.ok(sources);
    }

    // 添加自定义API源
    @PostMapping("addCustomProxy")
    public R<?> addCustomProxy(@RequestBody Map<String, Object> request, @CookieValue(value = "userId", required = false) String userId) {

        if (userId == null || userId.isEmpty()) {
            return R.fail("用户ID缺失");
        }

        String name = (String) request.get("name");
        String apiUrl = (String) request.get("apiUrl");
        String detailUrl = (String) request.get("detailUrl");
        boolean adult = Boolean.parseBoolean(request.get("adult").toString());

        if (name == null || name.trim().isEmpty()) {
            return R.fail("API名称不能为空");
        }

        if (apiUrl == null || apiUrl.trim().isEmpty()) {
            return R.fail("API URL不能为空");
        }

        proxyService.addCustomProxy(userId, name, apiUrl, detailUrl, adult);

        return R.ok("添加自定义API源成功");
    }

    // 更新自定义API源
    @PostMapping("updateCustomProxy")
    public R<?> updateCustomProxy(@PathVariable Long id, @RequestBody Map<String, Object> request, @CookieValue(value = "userId", required = false) String userId) {

        if (userId == null || userId.isEmpty()) {
            R.fail("用户ID缺失");
        }

        String name = (String) request.get("name");
        String apiUrl = (String) request.get("apiUrl");
        String detailUrl = (String) request.get("detailUrl");
        boolean adult = Boolean.parseBoolean(request.get("adult").toString());

        proxyService.updateCustomProxy(id, userId, name, apiUrl, detailUrl, adult);

        return R.ok("更新自定义API源成功");
    }

    // 删除自定义API源
    @PostMapping("deleteCustomProxy")
    public R<?> deleteCustomProxy(@PathVariable Long id, @CookieValue(value = "userId", required = false) String userId) {

        if (userId == null || userId.isEmpty()) {
            R.fail("用户ID缺失");
        }

        proxyService.deleteCustomProxy(id, userId);

        return R.ok("删除自定义API源成功");
    }
}